var param = new Object({
	init:function(){
		this.timeSize = ''
		this.beginDate = ''
		this.endDate = ''
		this.netYuan = ''
		this.dimension = ''
		this.city_code = ''
		this.tai = ''
		this.cell_id = ''
		this.enodeb_id = ''
		this.usergroup = ''
		this.user = ''
		this.terminal_brand = ''
		this.terminal_model = ''
		this.apn = ''
		this.tai_check = ''
		this.	enodebid_check = ''
		this.	cell_id_check = ''
	},
	setDimensionParam:function(dimension){
		switch(dimension) {
			case 'area' :
				this.city_code=$('#area').val()
				if($('#tai_check').is(':checked')) {
					this.tai_check = 1
				    this.tai = $('#tai').val()
				}
				if($('#enodebid_check').is(':checked')) {
					this.	enodebid_check = 1
					this.enodeb_id = $('#enodeb_id').val()
				}
				if($('#cell_id_check').is(':checked')) {
					this.	cell_id_check = 1
					this.cell_id = $('#cell_id').val()
				}
				break
			case 'usergroup' :
				this.usergroup = $('#groupId').val()
				break
			case 'user' :
				this.msisdn = $('#msisdn').val()
				break
			case 'terminal_type' :
				this.terminal_brand = $('#terminalBrandId').val()
				this.terminal_model = $('#terminalModelId').val()
				break
			case 'apn' :
				this.apn = $('#apnId').val()
				break
		}
	},
	setParam:function(){
		this.init()
		this.timeSize = $('input:radio[name="timeSize"]:checked').val();
		this.beginDate = $('#beginDate').val()
		this.endDate = $('#endDate').val()
		this.netYuan = $('#netYuan').val()
		this.dimension = $('input:radio[name="dimension"]:checked').val();
		this.setDimensionParam(this.dimension)
	}
});
var datatable;// 定义一个全局的dataTable
var attachChar;
var epsAttachChar;
var areaAttachChar;
var d;
$(function() {
	showArearList("area");
	$('#table tbody').on('click', 'tr', function() {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			datatable.$('tr.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});
	$("#groupId").combobox({width:40});
	$("#terminalBrandId").combobox({
		select:function(event,ui){
			loadTerminalModel();
		},
		width:100
	});
	$("#terminalModelId").combobox({width:100});  
	$("#apnId").combobox({width:40});
	$("#sgw").combobox({width:40});
	//图表  
	psLineChar = echarts.init(document.getElementById('psLine'));
	epsAttachChar = echarts.init(document.getElementById('epsAttachChar'));
	areaAttachChar = echarts.init(document.getElementById('areaAttachChar'));
	
	if($("#jspType").val() == 'kpi_analysis'){
		var kpi_timeSize = $('#kpi_timeSize').val();
		$("input[type='radio'][name=timeSize][value='"+kpi_timeSize+"']").attr("checked",true);
	}
//	getList();
})

function handleDimension () {
	var a=$("input[name='dimension']:checked");
	if(a.val()=='area'){
		$("#enodebid_check").attr("disabled",false);
		$("#tai_check").attr("disabled",false);
		$("#cell_id_check").attr("disabled",false);
		$("#enodeb_id").attr("readonly",false);
		$("#tai").attr("readonly",false);
		$("#cell_id").attr("readonly",false);
	} else {
		$("#enodebid_check").attr("disabled",true);
		$("#tai_check").attr("disabled",true);
		$("#cell_id_check").attr("disabled",true);
		$("#enodeb_id").attr("readonly",true);
		$("#tai").attr("readonly",true);
		$("#cell_id").attr("readonly",true);
	}
}

function handleCol() {
	var a=$("input[name='dimension']:checked");
	if(a.attr('data-column')!=undefined){
		if(a.val()=='area'){
			$("#enodebid_check").attr("disabled",false);
			$("#tai_check").attr("disabled",false);
			$("#cell_id_check").attr("disabled",false);
			$("#enodeb_id").attr("readonly",false);
			$("#tai").attr("readonly",false);
			$("#cell_id").attr("readonly",false);
			if($("#tai_check").is(':checked')){
				datatable.column($("#tai_check").attr('data-column')).visible(true);
			} else {
				datatable.column($("#tai_check").attr('data-column')).visible(false);
			}
			if($("#enodebid_check").is(':checked')){
				datatable.column($("#enodebid_check").attr('data-column')).visible(true);
			} else {
				datatable.column($("#enodebid_check").attr('data-column')).visible(false);
			}
			if($("#cell_id_check").is(':checked')){
				datatable.column($("#cell_id_check").attr('data-column')).visible(true);
			} else {
				datatable.column($("#cell_id_check").attr('data-column')).visible(false);
			}
		} 
		var column = datatable.column(a.attr('data-column'));
	    column.visible(true);
	}
}

function getList() {
	param.setParam()
	console.log(param)
	//$("#table tbody tr").remove();
	$("#tbodyId").remove();
	$('#table').append($("<tbody id='tbodyId'></tbody>"));
	if (datatable == undefined || datatable == "undefined" || datatable == null) {
	} else {
		datatable.destroy();
	}
	datatable = $('#table')
				.DataTable(
					{
						"aoColumnDefs" : [ {
							sDefaultContent : '',
							aTargets : [ '_all' ]
						} ],
						"bPaginate" : true, // 翻页功能
						"iDisplayLength" : 50,
						"bLengthChange" : false, // 改变每页显示数据数量
						"bFilter" : false, // 过滤功能
						"ordering": true, //排序功能
						"bSort" : true, // 排序功能
						"bInfo" : true,// 页脚信息
						"bAutoWidth" : true,// 自动宽度
						"iDisplayLength" : 50,
						// "sPaginationType": "full_numbers",//显示分页的样式
						// 还有simple_numbers等
//						"sScrollX" : "100%",
						"sScrollXInner" : "110%",
						"bScrollCollapse" : true,
						"oLanguage" : {
							"sLengthMenu" : "每页显示 _MENU_条",
							"sZeroRecords" : "没有找到符合条件的数据",
							"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
							"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
							"sInfoEmpty" : "没有记录",
							"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
							"sSearch" : "搜索：",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "前一页",
								"sNext" : "后一页",
								"sLast" : "尾页"
							}
						},
						"bProcessing" : true,
						"bServerSide" : true,
		"sAjaxSource" : "../../businessAnalysis/pdn/getPdnConnect",
		"fnServerData" : function(sSource, aoData, fnCallback) {
			$.ajax({
				"type" : "post",
				"contentType" : "application/x-www-form-urlencoded",
				"url" : sSource,
				"dataType" : "json",
				"data" : {
					"aoData" : JSON.stringify(aoData),
					"timeSize" : param.timeSize,
					"beginDate" : param.beginDate,
					"endDate" : param.endDate,
					"netYuan" : param.netYuan,
					"dimension" : param.dimension,
					"city_code" : param.city_code,
					"tai" : param.tai,
					"cell_id" : param.cell_id,
					"enodeb_id" : param.enodeb_id,
					"usergroup" : param.usergroup,
					"msisdn" : param.msisdn,
					"terminal_brand" : param.terminal_brand,
					"terminal_model" : param.terminal_model,
					"apn" : param.apn,
					"tai_check":param.tai_check,
					"enodebid_check":param.enodebid_check,
					"cell_id_check":param.cell_id_check
				}, // 以json格式传递
				"success" : function(resp) {
					fnCallback(resp.pageDate);
					d=resp.allData;
				}
			});
		},
		columns : [ {
			data : "report_date",
		}, { 
			data : "city_code",
			"visible": false
		}, {
			data : "tai",
			"visible": false
		},{
			data : "enodeb_name",
			"visible": false
		}, {
			data : "cell_name",
			"visible": false
		}, {
			data : "group_id",
			"visible": false
		}, {
			data : "msisdn",
			"visible": false
		}, {
			data : "terminal_model",
			"visible": false
		}, {
			data : "apn_name",
			"visible": false
		}, {
			data : "mme_name"
		}, {
			data : "count"
		}, {
			data : "failure_count",
			render:function(value,type,row){
				var dateType=$('input:radio[name="timeSize"]:checked').val();
				var failureType = "";
				var jspType = "pdnConnectAnalysis";
				var paramDate = row.report_date == undefined ? "" : row.report_date;
				var mme_name = row.mme_name == undefined ? "" : row.mme_name;
				var city_code = row.city_code == undefined ? "" : row.city_code;
				var tai = row.tai == undefined ? "" : row.tai;
				var enodeb_id = row.enodeb_id == undefined ? "" : row.enodeb_id;
				var enodeb_name =row.enodeb_name == undefined ? "" : row.enodeb_name;
				var cell_name =row.cell_name == undefined ? "" : row.cell_name;
				var group_id = row.group_id == undefined ? "" : row.group_id;
				var msisdn = row.msisdn == undefined ? "" : row.msisdn;
				var user_terminal = row.terminal_model == undefined ? "" : row.terminal_model;
				var terminal_brand = row.terminal_brand == undefined ? "" : row.terminal_brand;
				var apn_name = row.apn_name == undefined ? "" : row.apn_name;
				
				return "<a href='#' onclick=\"forwordInterfaceFailure('"
						+ dateType + "','"
						+ failureType +"','"
						+ jspType +"','"
						+ mme_name +"','"
						+ city_code +"','"
						+ tai +"','"
						+ enodeb_id +"','"
						+ enodeb_name +"','"
						+ cell_name +"','"
						+ group_id +"','"
						+ msisdn +"','"
						+ user_terminal +"','"
						+ terminal_brand +"','"
						+ apn_name +"','"
						+paramDate
						+"')\"  >"+value+"</a>";
			}
		}, {
			data : "success_ratio"
		} , {
			data : "latetime"
		} ]
	});
	handleCol();
}

function showPic(){
	var tab=$("#table");
	var pic=$("#pic");
	
	if(tab.is(":hidden")){
		tab.show();
		pic.hide();
	}else{
		tab.hide();
		pic.show();
		
		initPdnChar();
		initEpsSessionChar();
		initAreaSessionChar();
	}
}

function initPdnChar(){
	//查询  
	psLineChar.clear();
	psLineChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessAnalysis/pdn/getPdnChar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"allData" : JSON.stringify(d)
		},
		success : function(data) {
			psLineChar.setOption(data, true);
			psLineChar.hideLoading();
		}
	});
}

function initEpsPdnChar(){
	//查询  
	epsAttachChar.clear();
	epsAttachChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessAnalysis/pdn/getEpsPdnChar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"allData" : JSON.stringify(d)
		},
		success : function(data) {
			epsAttachChar.setOption(data, true);
			epsAttachChar.hideLoading();
		}
	});
}

function initAreaPdnChar(){
	//查询  
	areaAttachChar.clear();
	areaAttachChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessAnalysis/pdn/getAreaPdnChar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"allData" : JSON.stringify(d)
		},
		success : function(data) {
			areaAttachChar.setOption(data, true);
			areaAttachChar.hideLoading();
		}
	});
}

function resetQueryForm(){
	$('#queryForm')[0].reset(); 
}

function forwordInterfaceFailure(dateType,failureTypeInfo,jspType,mme_name,city_code,tai,
		enodeb_id,enodeb_name,cell_name,group_id,msisdn,user_terminal,terminal_brand,apn_name,paramDate) {
	var url = "/kpi/interfaceFailureLink/index?dateType=" + dateType +"&failureTypeInfo="+failureTypeInfo + "&jspType="+jspType +"&mme_name="+mme_name
	+ "&paramDate=" + paramDate + "&interfaceType=2&signalType=7&type=" + param.dimension +"&tac_status="+param.tac_status
	+ "&eNodeBID_status=" + param.eNodeBID_status +"&village_status="+param.village_status
	+ "&city_code=" +city_code+"&tai="+tai+"&enodeb_id="+enodeb_id+"&enodeb_name="+enodeb_name  
	+ "&cell_name=" +cell_name+"&group_id="+group_id+"&msisdn="+msisdn+"&user_terminal="+user_terminal+"&terminal_brand="+terminal_brand+"&apn_name="+apn_name;   
	linkTab("接口故障", url);
}

/*function forwordInterfaceFailure(dateType,paramDate){
	var url="/kpi/interfaceFailure/index?dateType="+dateType+"&paramDate="+paramDate+"&interfaceType=2&signalType=7";
	linkTab("接口故障",url)
}*/

//下拉树 begin
function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree
			.getSelectedNodes(), v = "";
	nodes.sort(function compare(a, b) {
		return a.id - b.id;
	});
	for (var i = 0, l = nodes.length; i < l; i++) {
		v += nodes[i].name + ",";
	}
	if (v.length > 0)
		v = v.substring(0, v.length - 1);
	var cityObj = $("#citySel");
	cityObj.attr("value", v);
	$("#area").val(treeNode.id);

	hideMenu();
}

/**
 * 显示树
 */
function showMenu() {
	var cityObj = $("#citySel");
	var cityOffset = $("#citySel").offset();
	$("#menuContent").css({
		left : cityOffset.left - 26 + "px",
		top : cityOffset.top - 206 + "px"
	}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}
/**
 * 隐藏树
 */
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}

/**
 * 根据终端类型，加载终端型号
 * 
 * @param parentid
 */
function loadTerminalModel() {
	var terminalBrand = $("#terminalBrandId").val();
	
	$.ajax({
		url : '../../feature/terminalUE/terminalModelInfos?terminalBrand='
				+ terminalBrand,
		type : 'POST',
		dataType : 'JSON',
		error : function() {
			alert('Error loading data!');
		},
		success : function(msg) {
			$("#terminalModelId").remove();
			$("#terminalModelTD").append($("<select id='terminalModelId' name='terminalModelId'></select>"));
			$("#terminalModelId").combobox();
			var list = msg.terminalModelList;
			$.each(eval(list), function(i, item) {
					$(
							"<option value='" + item.terminal_model + "'>" + item.terminal_model
							+ "</option>").appendTo(
									$("#terminalModelId"));
			});
		}
	});
	
}

function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
			event.target).parents("#menuContent").length > 0)) {
		hideMenu();
	}
}


function exportExcel() {
	$.ajax({
		type : "POST",
		url : "../../businessAnalysis/pdn/exportBigDataExcel?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			if (data.flag) {
				var url = "../../businessAnalysis/pdn/downFiles?path="
						+ data.path;
				window.location.href = url;
			} else {
				alert(data.msgs);
			}
		}
	});
}